<template>
  <div class="index">
    <!-- 顶部 -->
    <div class="top"><Admin></Admin></div>
    <!-- bottom -->
    <div class="bottom">
      <div class="left"><SideNav></SideNav></div>
      <div class="right">
        <div class="content">
          <el-scrollbar style="height: 100%;">
            <!-- 当前位置 -->
            <div class="current_position">
              <p class="breadcrumb">当前位置 :</p>
              <el-breadcrumb separator="/">
                <el-breadcrumb-item class="up">生活服务</el-breadcrumb-item>
                <el-breadcrumb-item>模板商品</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
            <!-- 列表 -->
            <div class="userlist">
              <div class="status_box">
                <div class="status">
                  <el-select v-model="value" placeholder="全部品类" @change="selectChanged()">
                    <el-option  label="全部品类" value="全部品类">全部品类</el-option>
                    <el-option v-for="(item,index) in options" :key="index" :label="item.label" :value="item.value">{{item.label}}</el-option>
                  </el-select>
                </div>
                <div class="input" @keyup.enter="findList()">
                  <el-input v-model="input" placeholder="产品名称/id/供应商" onfocus="this.placeholder=''" onblur="this.placeholder='产品名称/id/商家名'" clearable></el-input>
                </div>
                <div class="search"><el-button @click="findList()">搜索</el-button></div>
                <div class="search search1">
                  <i class="line"></i>
                  <el-button @click="add()">新增</el-button>
                </div>
              </div>
              <div class="table">
                <el-table ref="multipleTable" :data="tableData" border tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
                  <el-table-column type="selection" fixed="left" width="50"></el-table-column>
                  <el-table-column prop="id" label="ID"></el-table-column>
                  <el-table-column prop="url"  label="主图">
                    <template slot-scope="scope">
                      <el-popover
                        placement="right"
                        trigger="hover">
                        <img :src="scope.row.topPic" style="height: 300px;width: 300px"/>
                        <img slot="reference" :src="scope.row.topPic"  style="max-height: 50px;max-width: 50px;margin-top: 10px;">
                      </el-popover>
                    </template>
                  </el-table-column>
                  <el-table-column prop="name" label="信息标题" width="200">
                    <template slot-scope="scope">
                      <p v-html="scope.row.mouldName">{{scope.row.mouldName}}</p>
                    </template>
                  </el-table-column>
                  <el-table-column prop="parentName" label="一级目录"></el-table-column>
                  <el-table-column prop="flagFallPrice" label="起步价" sortable width="100">
                    <template slot-scope="scope">
                      <span class="redtext">{{scope.row.flagFallPrice}}元</span>
                    </template>
                  </el-table-column>
                  <el-table-column prop="fare" label="路费" sortable>
                    <template slot-scope="scope">
                      <span class="redtext">{{scope.row.fare}}元</span>
                    </template>
                  </el-table-column>
                  <el-table-column prop="releaseVolume" label="发布" sortable></el-table-column>
                  <el-table-column prop="salesVolume" label="销量" sortable></el-table-column>
                  <el-table-column prop="createTime" label="创建时间" :formatter="dateForma" sortable width="200"></el-table-column>
                  <el-table-column prop="updateTime" label="更新时间" :formatter="dateForma" width="200"></el-table-column>
                  <el-table-column  label="状态" sortable :sort-by="['status']">
                    <template slot-scope="scope">
                      <span v-if="scope.row.status==1" class="putaway">上架</span>
                      <span v-if="scope.row.status==2" class="soldout">下架</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" fixed="right"  width="150">
                    <template slot-scope="scope">
                      <el-button class="operation" @click="goToListDetails(scope.row)" type="text" size="small">编辑</el-button>
                      <el-button v-if="scope.row.status==1" class="operation" @click="soldOut(scope.row)" type="text" size="small">下架</el-button>
                      <el-button v-if="scope.row.status==2" @click="putaway(scope.row)" type="text" size="small">上架</el-button>
                      <el-button @click="deleteProduct(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="[50,100,150,200,250]"
                  :page-size="pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
                </el-pagination>
              </div>
            </div>
          </el-scrollbar>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  let moment = require("moment")
  import Admin from '@/components/Admin.vue'
  import SideNav from '@/components/SideNav.vue'

  export default {
    name: 'Template',
    components:{
      Admin,
      SideNav
    },
    data () {
      return {
        page: 1,//页码
        pageSize: 50,//每页条数
        total: 0,/* 总的条数 */
        currentPage: 0,/* 前往第几页 */
        options: [{//账号状态
          value: '1',
          label: '一类目',
        },{
          value: '2',
          label: '二类目',
        }],
        value: null,//账号状态
        input: null,// 搜索输入框
        tableData: [],// 表格
        multipleSelection: [], // 选择后的列表
      }
    },
    mounted() {
      this.findList();
    },
    methods: {
      // 时间格式过滤
      dateForma(row,column){
        var date = row[column.property];
        if(date == undefined){
          return ''
        }
        return moment(date).format("YYYY-MM-DD HH:mm:ss")
      },
      // 获取所有商品模板
      findList(){
        this.$axios.post(this.baseURL1+'admin/goodsMould/list',{
          "page":this.page,
          "size":this.pageSize,
          "requestSource":"PC"
        }).then((res)=>{
          console.log(res,"所有商品模板");
          if(res.data.code==1){
            this.tableData = res.data.data.records;
            this.total = res.data.data.total;
          }
        }).catch((err)=>{
          console.log('request fail', err);
        })
      },
      // 表格的全选
      handleSelectionChange(val) {
        console.log(val);
        this.multipleSelection = val;
      },
      // 选择的每页条数
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.findList();
      },
      // 当前页面
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.page = val;
        this.findList();
      },
      // 新增模板商品
      add(){
        this.$router.push({
          path: '/TemplateAdd',
        })
      },
      // 编辑该商品
      goToListDetails(row) {
        var data = row.id;
        this.$router.push({
          path: '/TemplateDeatil',
          query: {
            data: data,
          }
        })
      },
      //上架商品模板
      putaway(row){
        this.$confirm('确认上架该商品模板?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post(this.baseURL1+'admin/goodsMould/putShelf',{
            "id": row.id,
            "requestSource":"PC",
          }).then((res)=>{
            if(res.data.code==0){
              this.$message({
                type: 'success',
                message: '上架商品模板成功'
              });
              setTimeout(()=>{
                this.findList();
              },1000)
            }else{
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
          }).catch((err)=>{
            console.log('request fail', err);
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      //下架商品模板
      soldOut(row){
        this.$confirm('确认下架该商品模板?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post(this.baseURL1+'admin/goodsMould/offShelf',{
            "id": row.id,
            "requestSource":"PC",
          }).then((res)=>{
            if(res.data.code==0){
              this.$message({
                type: 'success',
                message: '下架商品模板成功'
              });
              setTimeout(()=>{
                this.findList();
              },1000)
            }else{
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
          }).catch((err)=>{
            console.log('request fail', err);
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      //删除该商品模板
      deleteProduct(row){
        this.$confirm('确认删除该商品模板?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post(this.baseURL1+'admin/goodsMould/offShelf',{
            "id": row.id,
            "requestSource":"PC",
          }).then((res)=>{
            if(res.data.code==0){
              this.$message({
                type: 'success',
                message: '删除商品模板成功'
              });
              setTimeout(()=>{
                this.findList();
              },1000)
            }else{
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
          }).catch((err)=>{
            console.log('request fail', err);
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
  @import '@/assets/css/index.scss'; //这个分号一定要写，要不会报错
  @import '@/assets/css/user.scss'; //这个分号一定要写，要不会报错
  .search{
    >>> .el-button{
      background-color: #fc7656!important;
    }
  }
  // 修改表格状态栏颜色
  >>> .el-table .cell .category{
    color: #409EFF;
  }
  >>> .el-table .cell .putaway{
    color: lightgreen;
  }
  >>> .el-table .cell .soldout{
    color: red;
  }
  >>> .el-table .cell .operation{
    color: #409EFF;
  }
  >>> .el-table .cell span{
    // padding: 0 3px;
    box-sizing: border-box;
  }
  >>> .el-table .cell{//表格超过2行显示省略号
    overflow:hidden;
    text-overflow:ellipsis;
    display:-webkit-box;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:2;
  }
  >>> .el-button+.el-button{
    margin-left: 0;
  }
  .search1{
    // background-color: #409eff;
    width: auto!important;
    .line{
      width: 1px;
      height: 32px;
      border-left: 1px solid #999;
      margin-right: 10px;
      display: inline-block;
      vertical-align: middle;
      font-style: normal;
    }
    >>> .el-button{
      background-color: #409eff!important;
    }
  }
  .redtext{
    color: red;
  }
</style>
